package com.snow.useviewdemo.util;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zhaoyingtao
 * Date: 2020/8/14
 * Describe:
 */
public class CountTimeUtils {
    /**
     * 延迟多长时间后开始每隔多少秒计数
     *
     * @param initialDelay 初始延迟时间 单位毫秒
     * @param period       间隔时间 单位毫秒
     *                     Disposable.dispose()停止计数,计数从0开始
     */

    public static Disposable startCountTime(int initialDelay, int period, CountTimeCallBack timeCallBack) {
        return Observable.interval(initialDelay, period, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(mm -> {
                    if (timeCallBack != null) {
                        timeCallBack.currentTimeNum(mm);
                    }
                });

    }

    public interface CountTimeCallBack {
        void currentTimeNum(Long nums);
    }
}
